package com.wanxi.company.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanxi.common.entity.BusinessLineModel;
import com.wanxi.common.result.ResultDTO;
import com.wanxi.common.util.Tool;
import com.wanxi.company.mapper.BusinessLineMapper;
import com.wanxi.company.service.BusinessLineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service(value = "businessService")

public class BusinessLineServiceImpl implements BusinessLineService {

    @Autowired
    private BusinessLineMapper businessLineMapper;

    @Override
    public ResultDTO add(BusinessLineModel businessLineModel) {

        LambdaQueryWrapper<BusinessLineModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(BusinessLineModel::getId)
                .eq(businessLineModel.getName()!=null,BusinessLineModel::getName,businessLineModel.getName());
        boolean exists = businessLineMapper.exists(queryWrapper);
        if (exists) {
            return ResultDTO.builder()
                    .code(0)
                    .msg("same name")
                    .count(-1)
                    .data("")
                    .build();
        } else {
            return ResultDTO.builder()
                    .code(0)
                    .msg("")
                    .count(businessLineMapper.insert(businessLineModel))
                    .data("")
                    .build();
        }
    }

    @Override
    public ResultDTO update(BusinessLineModel businessLineModel) {
        LambdaQueryWrapper<BusinessLineModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(BusinessLineModel::getId)
                .eq(businessLineModel.getName()!=null,BusinessLineModel::getName,businessLineModel.getName());
        boolean exists = businessLineMapper.exists(queryWrapper);
        if (exists) {
            return ResultDTO.builder()
                    .code(0)
                    .msg("")
                    .count(businessLineMapper.updateById(businessLineModel))
                    .data("")
                    .build();

        } else {
            return ResultDTO.builder()
                    .code(0)
                    .msg("have no")
                    .count(-1)
                    .data("")
                    .build();
        }
    }

    @Override
    public ResultDTO delete(BusinessLineModel businessLineModel) {
        return ResultDTO.builder()
                .code(0)
                .msg("")
                .count(businessLineMapper.deleteById(businessLineModel))
                .data("")
                .build();
    }

    @Override
    public ResultDTO findById(BusinessLineModel businessLineModel) {
        LambdaQueryWrapper<BusinessLineModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(BusinessLineModel::getId,BusinessLineModel::getName,BusinessLineModel::getImgHref,
                        BusinessLineModel::getContent,BusinessLineModel::getEnable)
                .eq(businessLineModel.getId() != null,BusinessLineModel::getId,businessLineModel.getId());
        BusinessLineModel businessLine = businessLineMapper.selectOne(queryWrapper);

        return ResultDTO.builder()
                .code(0)
                .msg("")
                .data(businessLine)
                .build();
    }

    @Override
    public ResultDTO findAll(BusinessLineModel businessLineModel) {

        businessLineModel.setPage(Tool.nullToPage(businessLineModel.getPage()));
        businessLineModel.setLimit(Tool.nullToLimit(businessLineModel.getLimit()));

        LambdaQueryWrapper<BusinessLineModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(BusinessLineModel::getId,BusinessLineModel::getName,BusinessLineModel::getImgHref,
                BusinessLineModel::getContent,BusinessLineModel::getEnable)
                .like(businessLineModel.getName()!=null && businessLineModel.getName()!="",BusinessLineModel::getName,businessLineModel.getName())
                .like(businessLineModel.getContent()!=null&& businessLineModel.getContent()!="",BusinessLineModel::getContent,businessLineModel.getContent())
                .eq(businessLineModel.getEnable()!=null&& businessLineModel.getEnable()!=-1,BusinessLineModel::getEnable,businessLineModel.getEnable());

        IPage<BusinessLineModel> page = new Page<>(businessLineModel.getPage(),businessLineModel.getLimit());
        IPage<BusinessLineModel> result = businessLineMapper.selectPage(page, queryWrapper);

        return ResultDTO.builder()
                .code(0)
                .msg("")
                .count((int) result.getTotal())
                .data(result.getRecords())
                .build();
    }

}
